﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Transactions;

namespace BPM.Web
{
	/// <summary>
	/// 使用此类标记后的Action，如果不抛出任何异常，则会提交事务。
	/// </summary>
	public class TransactionScopeAttribute : ActionFilterAttribute
	{
		private TransactionScope _scope;

		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			this._scope = new TransactionScope();

			base.OnActionExecuting(filterContext);
		}

		public override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			base.OnActionExecuted(filterContext);

			if (filterContext.Exception == null)
			{
				this._scope.Complete();
			}

			this._scope.Dispose();
		}
	}
}